/**
 * 插件控制台：用于独立插件管理
 */
Rsd.define('Rsd.zen.dialog.ConsoleDialog', {
    extend: 'Rsd.container.Dialog',
    requires: [
        'Rsd.control.Svg',
        'Rsd.control.MenuList',
        'Rsd.form.Link',
        'Rsd.form.Image',
        'Rsd.form.Button',
        'Rsd.form.Input',
        'Rsd.form.Label',
        'Rsd.form.ComboBox',
        'Rsd.form.ComboBoxEx',
        'Rsd.form.ComboBoxEnum',
        'Rsd.form.Time',
        'Rsd.form.Timestamp',
        'Rsd.zen.layout.Header',
        'Rsd.container.PageContainer'
    ],
    xtype: 'console-dialog',
    height: '95%',
    width: '95%',
    layout: 'border',
    autoLoad: false,
    /**
     * 插件
     */
    pulgin: null,
    /**
     * 
     */
    header: {
        height: 50,
        style: { backgroundColor: '#07051abf', color: '#fff' }
    },
    items: [
        //left 左侧一级菜单
        {
            xtype: 'menu-list',
            margin: '0 2 0 2',
            labelTagName: 'img',
            label: {
                xtype: 'image',
                label: false,
                position: 'top',
                visible: true,
                width: '100%',
                height: 'auto',
                space: 5,
                style: { minHeight: '50',  verticalAlign: 'middle' }
            },
            region: 'left',
            width: 100,
            usingItemHover: true,
            itemStyle: { height: '45px', lineHeight: '45px', "margin": '0px 2px 1px 0px' },
            itemClick: 'left_menu_click',
            itemSelectedCls: 'x-selected',
            layout: 'none',
            dataSource: [],
            border: false
        },
        //top 顶部一级菜单
        {
            id: "content-menu-list",
            xtype: 'menu-list',
            label: null,
            region: 'top',
            width: 'auto',
            cls: 'x-menu-list-h',
            itemSelectedCls: 'x-selected',
            usingItemHover: true,
            margin: '10 0 0 10',
            style: { backgroundColor: 'rgba(128, 128, 128, 0.05)', borderRadius: '6px' },
            "itemStyle": { "height": '50px', "width": '150px', "float": 'left', "textAlign": 'center', "marginRight": '2px' },
            itemClick: 'second_menu_click',
            height: 50,
            layout: 'none',
            border: false
        },
        //content 内容显示区
        {
            xtype: 'page-container',
            region: 'center',
            muliti: false,
            border: false,
            margin: '6 0 0 6',
            style: { backgroundColor: 'rgba(128, 128, 128, 0.05)', borderRadius: '6px', marginTop: '2px' }
        },
    ],
    /**
     * 数据
     */
    //data:null,
    /**
     * 
     * @param {*} config 
     */
    constructor: function ConsoleDialog(config) {
        config = config || {};
        this.apply(config);
        this.left.label.src = this.logo;
    },
    /**
     * 
     */
    load: function load() {

        if (!Rsd.isEmpty(this.plugin)) { 
            //加载菜单  
            this.left.loadData(this.plugin.menu);
            this.left.select(0);
            //console.log(this.left.label);  
            return;
        }
        if (!Rsd.isEmpty(this.menu)) {
            //加载菜单  
            this.left.loadData(this.menu);
            this.left.select(0);
            return;
        }
    },
    /**
     * 
     * @param {*} item 
     */
    left_menu_click: function left_menu_click(item) {
        var menu = item.menu;
        this.selectMenu(menu);
    },
    /**
     * @description 一级菜单被选中
     * @public
     * */
    selectMenu: function selectMenu(menu) {
        var me = this;
        if (menu instanceof Rsd.data.Menu) {

            this.block(function () {

                if (me.items[1] instanceof Rsd.control.MenuList) {
                    //layout second menu in top
                    me.items[1].removeAll();
                    me.items[1].loadData(menu);

                    if (menu.children.length > 0) {
                        me.items[1].select(0);
                        return;
                    }
                }

                //show first page in content
                if (!Rsd.isEmpty(menu.viewType)) {
                    me.createPage(menu, function (page) {
                        me.showPage(page);
                    });

                    return;
                }

                if (menu.handler) {
                    menu.funApplyByIOC(menu.handler);
                    return;
                }
                console.log('未设置Menu.viewType，无法打开页面',menu);
                Rsd.showMessage('未设置Menu.viewType，无法打开页面（' + menu.text + '）。');

            }, 1000, 'left_menu_click');
        }
        else {
            throw new Error(' argument menu type is not Rsd.data.Menu.')
        }

    },
    /**
    *@private
    * */
    second_menu_click: function second_menu_click(item) {
        var me = this;
       
        this.block(function () {

            me.createPage(item.menu, function (page) {
                if (page) {
                    me.showPage(page);
                }
            });
        }, 1000, 'second_menu_click');
    },
    /**
     * 
     * @param {*} className 
     * @param {*} config 
     */
    newPageObject: function newPageObject(className, config) {
        var page = null;
        var _msg = '';
        try {
            page = Rsd.create(className, config);
        }
        catch (ex) {
            _msg = 'Failed to open page:' + className + '=>' + ex.toString(); 
        }

        if (page == null) {
            page = Rsd.create('Rsd.zen.page.NotFoundPage', {text:_msg});
        }

        return page;
    },
    /**
     * @description 创建页面,
     * @param {*} menu menu 的 where属性的value 支持template 字符串，如:{name:'username',op:'like',value:'{{=merchUserName}}'}。
     * @returns 
     */
    createPage: function createPage(menu, callback) {

        var me = this;
        var _menu = menu; 
        var _viewType = _menu.viewType;
        if (Rsd.isEmpty(_viewType) && _menu.children && _menu.children.length > 0) {
            _menu = _menu.children[0];
            _viewType = _menu.viewType;
        }
        if (!_viewType) {
            console.error('viewType is null',_menu);
            return;     
        }
        if (_menu.where) {
            for (var i in _menu.where) {
                //仅字符串类型，进行模板字符串转化
                if (Rsd.isString(_menu.where[i].value)) {
                    _menu.where[i].value = Rsd.template(_menu.where[i].value, this.data);
                }
            }
        }
        var _path = _menu.text;
        if (_menu.parent) {
            _path = _menu.parent.text + '->' + _menu.text;
        }
        var _config = Rsd.apply({
            menu: _menu,
            tabTitle: _menu.text,
            path: _path,
            data: this.data
        }, _menu.args || {});

        if (_menu.modelType) {
            //console.log('提前预加载schema:'+_menu.modelType);
            Rsd.app.getSchema(_menu.modelType, function (schema) {
                //console.log(schema);
                _config.modelSchema = schema;
                var page = me.newPageObject(_viewType, _config);
                if (callback) {
                    callback(page);
                } 
            });

            return;
        }

        var page = me.newPageObject(_viewType, _config);
        if (callback) {
            callback(page);
        } 
 
    },
    /** 
     @description 展示页面
     * */
    showPage: function showPage(page, speed) {
        var _page = page;
        if (Rsd.isEmpty(page)) {
            _page = Rsd.create('Rsd.zen.page.NotFoundPage', { tabTitle: '错误', selected: true, text: "argumens page is null when call function showPage." });

        }
        if (_page instanceof Rsd.container.Page) {

        } else {
            console.error(page);
            _page = Rsd.create('Rsd.zen.page.NotFoundPage', { tabTitle: '错误', selected: true, text: "argumens page  is not instance of class 'Rsd.container.Page'.Please view console." });

        }

        _page.selected = true;
        //console.log(_page.layout);
        this.items[2].add(_page, speed || 0);
    },
    /*
    * */
    show: function show(animate, speed) {

        this.items[2].removeAll();

        if (arguments.length > 1) {
            this.callParent(animate, speed);
            return this;
        }
        if (arguments.length > 0) {
            this.callParent(arguments[0]);
            return this;
        }
        this.callParent();
        return this;
    },
    /*
    * */
    hideLeft: function hideLeft() {

        this.items[1].panelContainers[0].style.display = 'none';
        var _sep = this.items[1].separatorContainers[0];
        _sep.title = '单击展开';

    },
    /*
    * */
    showLeft: function showLeft() {
        this.items[1].panelContainers[0].style.display = '';
        var _sep = this.items[1].separatorContainers[0];
        _sep.title = '单击隐藏';
    }

}, function (type) {

    var _leftGetter = function () {
        return this.items[0];
    };

    var _leftSetter = function (value) {
        if (Rsd.isEmpty(value)) {
            return;
        }
        if (Rsd.isString(value)) {
            this.items[0].xtype = value;
            return;
        }
        if (Rsd.isObject(value)) {
            Rsd.apply(this.items[0], value, 3);
            return;
        }
    }
    //左侧
    this.defineProperty(type, "left", _leftGetter, _leftSetter, true);

    var _topGetter = function () {
        return this.items[1];
    };

    var _topSetter = function (value) {
        if (Rsd.isEmpty(value)) {
            return;
        }
        if (Rsd.isString(value)) {
            this.items[1].xtype = value;
            return;
        }
        if (Rsd.isObject(value)) {
            Rsd.apply(this.items[1], value, 3);
            return;
        }
    }
    //顶部
    this.defineProperty(type, "top", _topGetter, _topSetter, true);

    var _contentGetter = function () {

        return this.items[2];
    };

    var _contentSetter = function (value) {
        if (Rsd.isEmpty(value)) {
            return;
        }
        if (Rsd.isString(value)) {
            this.items[2].xtype = value;
            return;
        }
        if (Rsd.isObject(value)) {
            Rsd.apply(this.items[2], value, 3);
            return;
        }
    }
    // 内容显示区 组件
    this.defineProperty(type, "content", _contentGetter, _contentSetter, true);

})